#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>

using namespace std;

struct TreeNode
{
    int val;
    TreeNode * left;
    TreeNode * right;
    TreeNode() : val(0), left(nullptr), right(nullptr)
    {}
    TreeNode(int val) : val(val), left(nullptr), right(nullptr)
    {}
    TreeNode(int val, TreeNode * left, TreeNode * right) : val(val), left(left), right(right)
    {}
};


class Solution 
{
    public:
    void recursion(int depth, vector<vector<int>> & result, TreeNode * cur)
    {
        if(cur == nullptr)
        {
            return;
        }
        if(depth == result.size())
        {
            result.push_back(vector<int>());
        }
        result[depth].push_back(cur->val);
        recursion(depth + 1, result, cur->left);
        recursion(depth + 1, result, cur->right);
    }
    vector<vector<int>> levelOrderBottom(TreeNode* root) 
    {
        vector<vector<int>> result;
        if(root == nullptr)
        {
            return result;
        }
        int depth = 0;
        recursion(0, result, root);
        reverse(result.begin(),result.end());
        return result;
    }
};

// 辅助函数：创建二叉树节点
TreeNode* createNode(int val) {
    return new TreeNode(val);
}

// 辅助函数：打印层序遍历结果
void printResult(const vector<vector<int>>& result) {
    cout << "[";
    for (int i = 0; i < result.size(); i++) {
        cout << "[";
        for (int j = 0; j < result[i].size(); j++) {
            cout << result[i][j];
            if (j < result[i].size() - 1) cout << ",";
        }
        cout << "]";
        if (i < result.size() - 1) cout << ",";
    }
    cout << "]" << endl;
}

// 辅助函数：释放二叉树内存
void deleteTree(TreeNode* root) {
    if (root == nullptr) return;
    deleteTree(root->left);
    deleteTree(root->right);
    delete root;
}

int main() {
    Solution solution;
    
    // 测试案例1: LeetCode示例1 - [3,9,20,null,null,15,7]
    //       3
    //      / \
    //     9   20
    //        /  \
    //       15   7
    // 预期输出: [[15,7],[9,20],[3]]
    cout << "测试案例1: [3,9,20,null,null,15,7]" << endl;
    TreeNode* root1 = createNode(3);
    root1->left = createNode(9);
    root1->right = createNode(20);
    root1->right->left = createNode(15);
    root1->right->right = createNode(7);
    
    vector<vector<int>> result1 = solution.levelOrderBottom(root1);
    cout << "输出: ";
    printResult(result1);
    cout << "预期: [[15,7],[9,20],[3]]" << endl << endl;
    
    // 测试案例2: LeetCode示例2 - [1]
    cout << "测试案例2: [1]" << endl;
    TreeNode* root2 = createNode(1);
    
    vector<vector<int>> result2 = solution.levelOrderBottom(root2);
    cout << "输出: ";
    printResult(result2);
    cout << "预期: [[1]]" << endl << endl;
    
    // 测试案例3: LeetCode示例3 - []
    cout << "测试案例3: []" << endl;
    TreeNode* root3 = nullptr;
    
    vector<vector<int>> result3 = solution.levelOrderBottom(root3);
    cout << "输出: ";
    printResult(result3);
    cout << "预期: []" << endl << endl;
    
    // 测试案例4: 完全二叉树
    //       1
    //      / \
    //     2   3
    //    / \ / \
    //   4  5 6  7
    // 预期输出: [[4,5,6,7],[2,3],[1]]
    cout << "测试案例4: 完全二叉树 [1,2,3,4,5,6,7]" << endl;
    TreeNode* root4 = createNode(1);
    root4->left = createNode(2);
    root4->right = createNode(3);
    root4->left->left = createNode(4);
    root4->left->right = createNode(5);
    root4->right->left = createNode(6);
    root4->right->right = createNode(7);
    
    vector<vector<int>> result4 = solution.levelOrderBottom(root4);
    cout << "输出: ";
    printResult(result4);
    cout << "预期: [[4,5,6,7],[2,3],[1]]" << endl << endl;
    
    // 测试案例5: 左偏树
    //   1
    //  /
    // 2
    ///
    //3
    // 预期输出: [[3],[2],[1]]
    cout << "测试案例5: 左偏树 [1,2,null,3]" << endl;
    TreeNode* root5 = createNode(1);
    root5->left = createNode(2);
    root5->left->left = createNode(3);
    
    vector<vector<int>> result5 = solution.levelOrderBottom(root5);
    cout << "输出: ";
    printResult(result5);
    cout << "预期: [[3],[2],[1]]" << endl << endl;
    
    // 测试案例6: 右偏树
    // 1
    //  \
    //   2
    //    \
    //     3
    // 预期输出: [[3],[2],[1]]
    cout << "测试案例6: 右偏树 [1,null,2,null,3]" << endl;
    TreeNode* root6 = createNode(1);
    root6->right = createNode(2);
    root6->right->right = createNode(3);
    
    vector<vector<int>> result6 = solution.levelOrderBottom(root6);
    cout << "输出: ";
    printResult(result6);
    cout << "预期: [[3],[2],[1]]" << endl << endl;
    
    // 测试案例7: 更复杂的不平衡树
    //       1
    //      / \
    //     2   3
    //    /     \
    //   4       5
    //  /         \
    // 6           7
    // 预期输出: [[6,7],[4,5],[2,3],[1]]
    cout << "测试案例7: 不平衡树" << endl;
    TreeNode* root7 = createNode(1);
    root7->left = createNode(2);
    root7->right = createNode(3);
    root7->left->left = createNode(4);
    root7->right->right = createNode(5);
    root7->left->left->left = createNode(6);
    root7->right->right->right = createNode(7);
    
    vector<vector<int>> result7 = solution.levelOrderBottom(root7);
    cout << "输出: ";
    printResult(result7);
    cout << "预期: [[6,7],[4,5],[2,3],[1]]" << endl << endl;
    
    // 清理内存
    deleteTree(root1);
    deleteTree(root2);
    deleteTree(root4);
    deleteTree(root5);
    deleteTree(root6);
    deleteTree(root7);
    // root3 是 nullptr，不需要删除
    
    return 0;
}